package cn.tedu.ivos2.user.service.impl;

import cn.tedu.ivos2.base.exception.ServiceException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos2.user.pojo.dto.UserQuery;
import cn.tedu.ivos2.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos2.user.pojo.entity.User;
import cn.tedu.ivos2.user.pojo.vo.UserVO;
import cn.tedu.ivos2.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务参数：{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题，不能直接返回结果给前端，而是要抛出业务层异常，让全局异常处理器进行处理
        * 注意：一旦抛出异常，前端拿到的结果是全局异常处理器返回的，不走Controller! */
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("根据用户名查询到的用户信息：{}",userVO);
        return userVO;
    }

    @Override
    public void save(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId()==null){
            user.setCreateTime(new Date());
            user.setPassword("123456");
            userMapper.save(user);
        } else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }

    @Override
    public List<UserVO> list(UserQuery userQuery) {
        List<UserVO> list = userMapper.list(userQuery);
        return list;
    }

    @Override
    public void updatePassword(Long id) {
        User user = new User();
        user.setId(id);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long id, String status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    public List<UserVO> queryAudit(Long id) {
        ArrayList<UserVO> userVOList = new ArrayList<>();
        UserVO userVO = userMapper.selectById(id);
        userVOList.add(userVO);
        if(userVO != null && userVO.getParentId() != null){
            UserVO userVO2 = userMapper.selectById(userVO.getParentId());
            userVOList.add(userVO2);
        }
        return userVOList;
    }
}
